package com.run_java_test.generator;


import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.run_java_test.RunJavaCodeAPP;
import com.run_java_test.generator.config.XMLTemplateEngine;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
import java.util.function.BiConsumer;
import java.util.function.Function;

@SpringBootTest(classes = {RunJavaCodeAPP.class})
public class FastAutoGeneratorTest2 {

    private final static String url = "jdbc:mysql://127.0.0.1:3306/test_security?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai";
    private final static String username = "root";
    private final static String password = "123";
    private final static String Project_Package = "com.run_java_test.";

    private static List<String> getTables(String tables) {
        return "all".equals(tables) ? Collections.emptyList() : Arrays.asList(tables.split(","));
    }

    public static void main(String[] args) {
        System.out.println("url:" + url);
        System.out.println("username:" + username);
        System.out.println("password:" + password);
        System.out.println("Project_Package:" + Project_Package);
        System.out.println("-------------------------------------------");
        System.out.println("Start generating code");

        Scanner scan = new Scanner(System.in);
        System.out.println("Table Name:");
        // nextLine() accepts input of type string
        String tbName = scan.nextLine();

        // tablename
        List<String> tableNames = FastAutoGeneratorTest2.getTables(tbName);

        // Segmentation results
        for (int i = 0; i < tableNames.size(); i++) {
            String tableName = tableNames.get(i);
            String moduleName = tableName.replaceAll("_", ".");

            FastAutoGenerator.create(new DataSourceConfig.Builder(url, username, password))
                    // Global configuration
                    .globalConfig(
                            new BiConsumer<Function<String, String>, GlobalConfig.Builder>() {
                                @Override
                                public void accept(Function<String, String> scanner, GlobalConfig.Builder builder) {
                                    builder.author("test").outputDir("C:/");
                                }
                            })
                    // Packet configuration
                    .packageConfig(
                            new BiConsumer<Function<String, String>, PackageConfig.Builder>() {
                                @Override
                                public void accept(Function<String, String> scanner, PackageConfig.Builder builder) {
                                    builder.parent(Project_Package.concat(moduleName));
                                }
                            })
                    // Policy configuration
                    .strategyConfig(
                            new BiConsumer<Function<String, String>, StrategyConfig.Builder>() {
                                @Override
                                public void accept(Function<String, String> scanner, StrategyConfig.Builder builder) {
                                    builder.addInclude(tableName)
                                            .controllerBuilder().enableRestStyle().enableHyphenStyle()
                                            .entityBuilder().enableLombok().idType(IdType.ASSIGN_ID).disableSerialVersionUID().build();
                                }
                            })
                    // Generate the template used by the code
                    .templateEngine(new XMLTemplateEngine())
                    // start execution
                    .execute();
        }
    }

}
